define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'caring_task/index' + location.search,
                    add_url: 'caring_task/add',
                    edit_url: 'caring_task/edit',
                    del_url: 'caring_task/del',
                    multi_url: 'caring_task/multi',
                    import_url: 'caring_task/import',
                    table: 'caring_task',
                }
            });

            var table = $("#table");
            $(".btn-add").data("area", ["100%", "100%"]);
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                // showExport:false,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                exportOptions: {
                    fileName: '關懷任務列表_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: ['user.avatar','operate'], //不导出字段
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'user.nickname', title: __('User.nickname'), operate: false},
                        {field: 'region_address', title: __('Region_address'), operate: false},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'caring_task/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'caring_task/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'caring_task/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            $(document).on('click','.spec_add_btn', function (event) {
                var user_ids = '';
                $('#tag_div .tag').each(function(){
                    user_ids =  user_ids+ $(this).attr('attr-id')+',';
                })
                var reg=/,$/gi;//此处是正则
                user_ids=user_ids.replace(reg,"");
                var url = $(this).attr('data-url')+'/ids/'+user_ids;
                if(!url) return false;
                var msg = $(this).attr('data-title');
                var width = $(this).attr('data-width');
                var height = $(this).attr('data-height');
                var area = [$(window).width() > 800 ? (width?width:'800px') : '65%', $(window).height() > 600 ? (height?height:'600px') : '65%'];
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: area,
                    callback:function(data){

                        $("#tag_div").html('');
                        $.each(data,function(index,value){
                            Controller.selectTag(value)
                        });
                        // CallBackFun(value.id, value.name);//在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
                    }
                };
                Fast.api.open(url,msg,options);
            });
            $(document).on('click','.spec_adds_btn', function (event) {
                var user_ids = '';
                $('#tag_divs .tags').each(function(){
                    user_ids =  user_ids+ $(this).attr('attr-id')+',';
                })
                console.log(user_ids);
                var reg=/,$/gi;//此处是正则
                user_ids=user_ids.replace(reg,"");
                var url = $(this).attr('data-url')+'/ids/'+user_ids;
                if(!url) return false;
                var msg = $(this).attr('data-title');
                var width = $(this).attr('data-width');
                var height = $(this).attr('data-height');
                var area = [$(window).width() > 800 ? (width?width:'800px') : '65%', $(window).height() > 600 ? (height?height:'600px') : '65%'];
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: area,
                    callback:function(data){

                        $("#tag_divs").html('');
                        $.each(data,function(index,value){
                            Controller.selectTags(value)
                        });
                        // CallBackFun(value.id, value.name);//在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
                    }
                };
                Fast.api.open(url,msg,options);
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        selectTag: function (obj) {
            var user_id = obj.id;
            if ( user_id != 0 ) {
                var html = '<span class="tag btn btn-default" attr-id="'+user_id+'"><span>'+obj.nickname+'</span><a href="javascript:;" title="移除标签" onclick="removeParent(this)">×</a><input type="hidden" name="labels[]" value="'+user_id+'"></span>';
                $("#tag_div").append(html);
                $("#tag_f_div").show();
            }
        },
        selectTags: function (obj) {
            var user_id = obj.id;
            if ( user_id != 0 ) {
                var html = '<span class="tags btn btn-default" attr-id="'+user_id+'"><span>'+obj.name+'</span><a href="javascript:;" title="移除标签" onclick="removeParents(this)">×</a><input type="hidden" name="labelss[]" value="'+user_id+'"></span>';
                $("#tag_divs").append(html);
                $("#tag_f_divs").show();
            }
        },
        user_record: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'caring_task/user_record/ids/'+Config.ids + location.search,
                    // add_url: 'caring_task/add',
                    // edit_url: 'caring_task/edit',
                    // del_url: 'caring_task/del',
                    // multi_url: 'caring_task/multi',
                    // import_url: 'caring_task/import',
                    table: 'caring_task',
                }
            });

            var table = $("#table");
            $(".btn-add").data("area", ["100%", "100%"]);
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: false,
                search:false,
                showColumns:false,
                showExport:false,
                commonSearch:false,
                pageSize: 10,
                showToggle: false,
                columns: [
                    [
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'nickname', title: __('User.nickname'), operate: false},
                        {field: 'region_address', title: __('Region_address'), operate: false},
                        {field: 'arrivaltime', title: __('Arrivaltime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'peoplenum', title: '需求人數', operate: false},
                        {field: 'score', title: '積分獎勵', operate: false},
                        {field: 'workinghours', title: '工時獎勵', operate: false},
                        {field: 'order_receiving_time', title: '任務領取時間', operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});